package com.ms.p2p.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.entity.member.Member;
import com.ms.p2p.enums.LoanStatus;
import com.ms.p2p.provider.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping({ "/member/deal" })
public class MemberDealController extends BaseController {

	@Reference
	private LoanTenderRecordProvider loanTenderRecordProvider;
	@Reference
	private LoanProvider loanProvider;
	@Reference
	private LoanStayProvider loanStayProvider;
	@Reference
	private LoanCategoryProvider loanCategoryProvider;
	@Reference
	private LoanDueInProvider loanDueInProvider;

	@RequestMapping("/tender")
	public String tenderlist(HttpServletRequest request,PageBean page) throws Exception {
		Member user = getCurrentUser();

		List<Map<String, Object>> loans = loanTenderRecordProvider.findPager(user.getId(),null,page);
		page.setTotalCount(loanTenderRecordProvider
				.findCount(user.getId(),null));
		request.setAttribute("loans", loans);

		request.setAttribute("page", page);
		request.setAttribute("borrowTypes", loanCategoryProvider.findAll());
		return "/customer/member/loan/tender/list";
	}
	@RequestMapping("/bidding")
	public String bidding(HttpServletRequest request,PageBean page) throws Exception {
		Member user = getCurrentUser();
		List<Map<String, Object>> loans = loanTenderRecordProvider.findLoanPager(user.getId(), LoanStatus.STATUS_3.getValue()+"",page);
		page.setTotalCount(loanTenderRecordProvider
				.findLoanCount(user.getId(),LoanStatus.STATUS_3.getValue()+""));
		request.setAttribute("loans", loans);
		request.setAttribute("page", page);
		request.setAttribute("borrowTypes", loanCategoryProvider.findAll());
		return "/customer/member/loan/tender/bidding";
	}
	@RequestMapping("/collection")
	public String collection(HttpServletRequest request,PageBean page) throws Exception {
		Member user = getCurrentUser();

		List<Map<String, Object>> borrows = loanTenderRecordProvider.findLoanPager(user.getId(), LoanStatus.STATUS_5.getValue()+"",page);
		page.setTotalCount(loanTenderRecordProvider
				.findLoanCount(user.getId(),LoanStatus.STATUS_5.getValue()+""));
		request.setAttribute("borrows", borrows);

		request.setAttribute("page", page);
		request.setAttribute("borrowTypes", loanCategoryProvider.findAll());
		return "/customer/member/loan/tender/collection";
	}

	@RequestMapping("/success")
	public String success(HttpServletRequest request,PageBean page) throws Exception {
		Member user = getCurrentUser();
		List<Map<String, Object>> loans = loanTenderRecordProvider.findLoanPager(user.getId(), LoanStatus.STATUS_6.getValue()+"",page);
		page.setTotalCount(loanTenderRecordProvider
				.findLoanCount(user.getId(),LoanStatus.STATUS_6.getValue()+""));
		request.setAttribute("loans", loans);
		request.setAttribute("page", page);
		request.setAttribute("borrowTypes", loanCategoryProvider.findAll());
		return "/customer/member/loan/tender/success";
	}






	/**
	 * 查询待收和投标记录
	 */
	@RequestMapping({ "/duein" })
	public String collectionList(HttpServletRequest request,
								 HttpServletResponse response, HttpSession session,String billStatus) throws Exception {
		Member user = getCurrentUser();
		String type = request.getParameter("type");
		PageBean page = new PageBean();
		String currentPage = request.getParameter("currentPage");
		if (currentPage != null && !("".equals(currentPage))) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		List<Map<String, Object>> collections = loanDueInProvider.findPager(
				user.getId(),billStatus, page);
		page.setTotalCount(loanDueInProvider.findCount(user.getId(),billStatus));
		request.setAttribute("collections", collections);
		request.setAttribute("billStatus",billStatus);
		request.setAttribute("page", page);
		request.setAttribute("borrowTypes", loanCategoryProvider.findAll());
		return "/customer/member/loan/duein/list";
	}



}
